package com.itheima.health.config;


import com.itheima.health.job.QuartzJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

    //1.创建job对象
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //关联我们自己的job类
        factoryBean.setJobClass(QuartzJob.class);
        return factoryBean;
    }

//    //2.创建trigger对象
//    //简单trigger
//    @Bean
//    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
//        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
//        //关联上边的jobdetail对象
//        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
//        //设置触发时间 2秒
//        factoryBean.setRepeatInterval(2000);
//        //设置重复次数
//        factoryBean.setRepeatCount(5);
//        return factoryBean;
//    }

    //cron trigger
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        //设置触发时间
        factoryBean.setCronExpression("0 0/2 * * * ?");
        //0 0 0 L * ?   本月最后一天
        return factoryBean;
    }

    //3.创建scheduled对象
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactoty myAdaptableJobFactoty) {
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        //关联trigger
        factoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        factoryBean.setJobFactory(myAdaptableJobFactoty);//调用createJobInstance方法

        return factoryBean;
    }
}
